import 'package:fish_redux/fish_redux.dart';
import 'package:www.znpigai.teacher/homework_detail_page/item/state.dart';
import 'package:www.znpigai.teacher/utils/http.dart';
import 'package:www.znpigai.teacher/utils/progress_dialog.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';

Effect<HomeworkAnswerState> buildEffect() {
  return combineEffects(<Object, Effect<HomeworkAnswerState>>{
    AnswerMarkAction.onMark: _onMark,
    AnswerMarkAction.onReWrite: _onReWrite,
  });
}

void _onMark(Action action, Context<HomeworkAnswerState> ctx) async {
  String remark = ctx.state.contentEditController.text;
  String score = ctx.state.scoreEditController.text;
  if (remark.isEmpty) {
    Util.alert("评语不能为空", ctx.context);
    return;
  }

  if (score.isEmpty) {
    Util.alert("分数不能为空", ctx.context);
    return;
  }

  if (ctx.state.rating1 == 0) {
    Util.alert("内容评价不能为空", ctx.context);
    return;
  }

  if (ctx.state.rating2 == 0) {
    Util.alert("表达评价不能为空", ctx.context);
    return;
  }

  if (ctx.state.rating3 == 0) {
    Util.alert("发展评价不能为空", ctx.context);
    return;
  }

  var pr = new ProgressDialog(ctx.context)..setMessage('请等待...');
  pr.show();

  var url = '/correct/teacher/api/homework/review/save.mvc';
  final _param = {
    'studentHomeworkId': ctx.state.id,
    'teacherScore': score,
    'categoryLevel1': 'level${ctx.state.rating1.toInt()}',
    'categoryLevel2': 'level${ctx.state.rating2.toInt()}',
    'categoryLevel3': 'level${ctx.state.rating3.toInt()}',
    'teacherRemark': remark,
  };
  Http.post(url, data: _param).then((result) {
    pr.hide();
    if (result.success) {
      Util.alert("作文批阅成功", ctx.context);
    } else {
      Util.alert(result.errorMsg, ctx.context);
    }
  });
}

void _onReWrite(Action action, Context<HomeworkAnswerState> ctx) async {
  Util.alert('API尚未提供', ctx.context); // TODO
}
